package com.xyhuo.leetcode.november;

import java.util.*;

/**
 * 两束之和
 * https://leetcode.cn/problems/two-sum/
 *
 * @time 2023/11/20
 */
public class TwoSum {

    public int[] twoSum(int[] nums, int target) {
        int length = nums.length;
        Set<Integer> numSet = new HashSet<>();
        for (int num : nums) {
            numSet.add(num);
        }
        for (int i = 0; i < length; i++) {
            int tmp = target - nums[i];
            if (!numSet.contains(tmp)) {
                continue;
            }
            for (int j = 0; j < length; j++) {
                if (j == i) {
                    continue;
                }
                if (nums[j] == tmp) {
                    int[] result = new int[2];
                    result[0] = i;
                    result[1] = j;
                    return result;
                }
            }
        }
        return null;
    }

}
